#include "BLockQueue.hpp"
#include <ctime>
#include <unistd.h>
#include <pthread.h>

void *ConsumerRun(void *args)
{
    BlockQueue<int> *bq = (BlockQueue<int> *)args;
    while (true)
    {
        // 获取数据/任务
        int data = 0;
        // 处理任务
        bq->Pop(&data);
        std::cout << "Consumer-> " << data << std::endl;

    }
    return nullptr;
}

void *ProducerRun(void *args)
{
    BlockQueue<int> *bq = (BlockQueue<int> *)args;

    srand(time(nullptr));
    while (true)
    {
        // 制造数据/任务
        int data = rand() % 10 + 1;
        std::cout << "Producer-> " << data << std::endl;
        // 添加数据/任务
        bq->Push(data);
        sleep(1);
    }

    return nullptr;
}

int main()
{
    BlockQueue<int> *blockqueue = new BlockQueue<int>();
    pthread_t consumer;
    pthread_t producer;

    pthread_create(&consumer, nullptr, ConsumerRun, (void *)blockqueue);
    pthread_create(&producer, nullptr, ProducerRun, (void *)blockqueue);

    pthread_join(consumer, nullptr);
    pthread_join(producer, nullptr);

    return 0;
}